package com.example.saiweisi.service.impl.userService;

import com.example.saiweisi.controller.request.*;
import com.example.saiweisi.mapper.systemAdminMapper.DeskMapper;
import com.example.saiweisi.mapper.systemAdminMapper.YuyueMapper;
import com.example.saiweisi.mapper.userMapper.OrderMapper;
import com.example.saiweisi.model.*;
import com.example.saiweisi.service.interf.IOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderService implements IOrderService {
    @Resource
    OrderMapper orderMapper;
    @Resource
    DeskMapper deskMapper;
    @Resource
    YuyueMapper yuyueMapper;
    @Override
    public PageInfo<Order> getHistoryOrder(HistoryOrderRequest historyOrderRequest) {
        PageHelper.startPage(historyOrderRequest.getPageNum(), historyOrderRequest.getPageSize());
        List<Order> orderList = orderMapper.getHistoryOrder(historyOrderRequest);
        for (Order order:orderList) {
            if (order.getFinishTime()!=null){
                order.setStatus("已完成");
            }else if(order.getIsPay() == 1){
                order.setStatus("已支付");
            }else if(order.getSubmitTime() != null){
                order.setStatus("已提交");
            }else {
                order.setStatus("错误");
            }
        }
        return new PageInfo<>(orderList);



    }

    @Override
    public void deleteOrder(Integer orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderIsdelete(1);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void payOrder(Integer orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setIsPay(1);
        order.setFinishTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
    }

    public Map<String,String> yuyue(Integer userId, YuyueRequest request) {
        Desk desk = new Desk();
        desk.setPosition(Integer.valueOf(request.getPosition()));
        desk.setCapacity(Integer.valueOf(request.getCount()));
        List<Desk> selectbycountandposition = deskMapper.selectbycountandposition(desk);
        List<Integer> ids =  yuyueMapper.selectuse();
        List<Desk> filteredDesks = selectbycountandposition.stream()
                .filter(filterdesk -> !ids.contains(filterdesk.getDeskId())) // 过滤掉ID存在于ids列表中的Desk对象
                .collect(Collectors.toList());
        Yuyue yuyue = new Yuyue();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(request.getTime(), formatter);
        Instant instant = zonedDateTime.toInstant();

        // 将Instant转换为java.util.Date
        Date date = Date.from(instant);

        yuyue.setYuyueTime(date);
        yuyue.setDeskId(filteredDesks.get(0).getDeskId());
        yuyue.setYuyueType(Integer.valueOf(request.getType()));
        yuyue.setYuyueStatus(0);
        yuyue.setUserId(String.valueOf(userId));
        yuyueMapper.insertSelective(yuyue);
        Map map = new HashMap();
        map.put("yuyueId",yuyue.getId());
        map.put("deskId",filteredDesks.get(0).getDeskId());
        return map;
    }

    public PageInfo<Yuyue> getHistoryYuyue(HistoryOrderRequest historyOrderRequest){
        PageHelper.startPage(historyOrderRequest.getPageNum(), historyOrderRequest.getPageSize());
        List<Yuyue> yuyueList = yuyueMapper.getHistoryYuyue(historyOrderRequest);
        return new PageInfo<>(yuyueList);
    }
    public PageInfo<Yuyue> getAll(YueyueRequest historyOrderRequest){
        PageHelper.startPage(historyOrderRequest.getPageNum(), historyOrderRequest.getPageSize());
        List<Yuyue> yuyueList = yuyueMapper.getAll(historyOrderRequest);
        return new PageInfo<>(yuyueList);
    }
    public void deleteyuyue(Integer id) {
        Yuyue yuyue = new Yuyue();
        yuyue.setId(id);
        yuyue.setYuyueStatus(1);
        yuyueMapper.updateByPrimaryKeySelective(yuyue);
    }

    @Override
    public PageInfo<OrderClothesKindMaterial> getOrderInfo(OrderInfoRequest orderInfoRequest) {
        PageHelper.startPage(orderInfoRequest.getPageNum(), orderInfoRequest.getPageSize());
        List<OrderClothesKindMaterial> caipins = orderMapper.getOrderInfo(orderInfoRequest);
        return new PageInfo<>(caipins);
    }
    @Override
    public void wan(Integer yuyueId) {
        Yuyue yuyue = new Yuyue();
        yuyue.setId(yuyueId);
        yuyue.setYuyueStatus(1);
        yuyueMapper.updateByPrimaryKeySelective(yuyue);
    }

//    @Override
//    public void submitOrder(Integer userId, Double orderPrice, Double orderPriceZhe, List<OrderRequest> orderRequest) {
//        Date date = new Date();
////        得到时间戳
//        date.getTime();
////        生成指定格式的时间字符串
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date2 = dateFormat.format(date);
//        //创建一个订单对象，方便插入时返回主键到对象中去
//        Order order=new Order((long)userId,date2,orderPrice,orderPriceZhe);
//        orderMapper.saveOrder(order);
//        orderRequest.forEach(e->{
//            Clothes clothes = new Clothes(userId,e.getClothesKind().getClothesKindId(),e.getClothesMaterial().getClothesMaterialId());
//
//            if(!e.getIshad()){
//                clothesMapper.saveClothes(clothes);
//                OrderClo orderClo = new OrderClo(order.getOrderId(),clothes.getClothesId());
//                orderMapper.saveOrderClo(orderClo);
//            }else {
//                OrderClo orderClo =new OrderClo(order.getOrderId(),e.getClothesId());
//                orderMapper.saveOrderClo(orderClo);
//                clothesMapper.setStepZero(e.getClothesId());
//            }
//
//        });
//    }
//
//    @Override
//    public void confrimWashing(Integer orderId) {
//        Date date = new Date();
////        得到时间戳
//        date.getTime();
////        生成指定格式的时间字符串
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String deliveryTime = dateFormat.format(date);
//        //为此订单添加送洗时间
//        orderMapper.confrimWashing(orderId,deliveryTime);
//        //将此订单下所有的衣物的状态设置为在店且未完成
//        //1）先根据订单id查出关联衣物id
//        List<Integer> clothesIdlist =  orderMapper.getClothesIdByOrderId(orderId);
//        //2)遍历衣物id,修改衣物状态
//        for (Integer clothesId : clothesIdlist) {
//            clothesMapper.setFinishAndTakeZero(clothesId);
//        }
//    }
//
//    @Override
//    public void confrimTaking(Integer orderId) {
//        Date date = new Date();
////        得到时间戳
//        date.getTime();
////        生成指定格式的时间字符串
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String finishTime = dateFormat.format(date);
//        orderMapper.confrimTaking(orderId,finishTime);
//    }
//
    @Override
    public PageInfo<OrderUser> getAllOrder(PageBaseRequest pageBaseRequest) {
        PageHelper.startPage(pageBaseRequest.getPageNum(), pageBaseRequest.getPageSize());
        List<OrderUser> orderList = orderMapper.getAllOrder();
        for (OrderUser order:orderList) {
            if (order.getFinishTime()!=null){
                order.setStatus("已完成");
            }else if(order.getIsPay() == 1){
                order.setStatus("已完成");
            }else if(order.getSubmitTime() != null){
                order.setStatus("已提交");
            }else {
                order.setStatus("错误");
            }
        }
        return new PageInfo<>(orderList);

    }

    @Override
    public PageInfo<OrderUser> getyuyueOrder(YuyueOrderRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<OrderUser> orderList = orderMapper.getyuyueOrder(request);
        for (OrderUser order:orderList) {
            if (order.getFinishTime()!=null){
                order.setStatus("已完成");
            }else if(order.getIsPay() == 1){
                order.setStatus("已完成");
            }else if(order.getSubmitTime() != null){
                order.setStatus("已提交");
            }else {
                order.setStatus("错误");
            }
        }
        return new PageInfo<>(orderList);

    }
}
